package com.fenghongzhang.login;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends Fragment {
    private EditText loginName;
    private EditText loginPwd;
    private Button login;
    private boolean flag = false;
    private SQLiteDatabase db;
    public LoginFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_login, container, false);

        MySqlHelper mySqlHelper = new MySqlHelper(getActivity(), "user.db", null, 1);
         db = mySqlHelper.getReadableDatabase();

        loginName = (EditText) inflate.findViewById(R.id.login_name);
        loginPwd = (EditText) inflate.findViewById(R.id.login_pwd);
        login = (Button) inflate.findViewById(R.id.login);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = loginName.getText().toString();
                String pwd = loginPwd.getText().toString();

                //和数据库中的值比对.
                 Cursor user = db.query("user", null, null, null, null,null,null);
                if(user != null){
                    while(user.moveToNext()){
                        String username = user.getString(user.getColumnIndex("username"));
                        String password = user.getString(user.getColumnIndex("pwd"));

                        //如果
                        if(username.equals(name) && password.equals(pwd)){
                            //登录成功,主页面
                            Intent intent = new Intent(getActivity(), Main2Activity.class);
                            startActivity(intent);
                            flag = true;
                        }
                    }
                    
//                    if(flag){
//                        Toast.makeText(, "", Toast.LENGTH_SHORT).show();
//                    }else{
//                        Toast.makeText(, "", Toast.LENGTH_SHORT).show();
//                    }
                }
            }
        });


        return inflate;
    }
}
